﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using He.Common;
using System.Collections;

namespace He.Web.UI
{
    public class Upload
    {
        private Model.webset webset;
        private string filePath; //上传目录名
        private readonly string fileType; //文件类型
        private readonly int fileSize; //文件大小0为不限制
        private readonly int waterMarkType; //图片水印状态
        private readonly int waterQuality; //水印图片质量
        private readonly string imgWaterPath; //水印图片地址
        private readonly int waterTransparency; //水印图片透时度
        private readonly string textWater; //水印文字
        private readonly string textWaterFont; //文字水印字体
        private readonly int textFontSize; //文字大小

        public Upload()
        {
            webset = new He.BLL.webset().GetWebset();
            filePath = webset.web_path + webset.upload_path;
            fileType = webset.upload_file_type;
            fileSize = webset.upload_file_size;
            waterMarkType = webset.watermarktype;
            waterQuality = webset.watermarkimgquality;
            imgWaterPath = webset.web_path + webset.watermarkpic;
            waterTransparency = webset.watermarktransparency;
            textWater = webset.watermarktext;
            textWaterFont = webset.watermarkfont;
            textFontSize = webset.watermarkfontsize;
        }

        ///<summary>
        /// 文件上传方法(根据时间命名保存路径名)
        /// </summary>
        public string fileSaveAs(HttpPostedFile postedFile)
        {
            try
            {
                string fileExt = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".") + 1);

                //验证合法的文件
                if (!FileHelper.CheckFileExt(this.fileType, fileExt))
                {
                    return Json(0, "不允许上传" + fileExt + "类型的文件！");
                }
                if (this.fileSize > 0 && postedFile.ContentLength > fileSize * 1024)
                {
                    return Json(0, "文件超过限制的大小啦！");
                }
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + fileExt; //随机文件名
                //检查保存的路径 是否有/开头结尾
                if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
                if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
                //按日期归类保存
                string datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
                this.filePath += datePath;
                //获得要保存的文件路径
                string serverFileName = this.filePath + fileName;
                //物理完整路径                    
                string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
                //检查是否有该路径没有就创建
                if (!Directory.Exists(toFileFullPath))
                {
                    Directory.CreateDirectory(toFileFullPath);
                }
                //将要保存的完整文件名                
                string toFile = toFileFullPath + fileName;
                //保存文件
                postedFile.SaveAs(toFile);

                //是否打图片水印
                if (IsWaterMark(fileExt) && waterMarkType > 0)
                {
                    switch (this.webset.watermarktype)
                    {
                        case 1:
                            WaterMark.AddImageSignText(serverFileName, serverFileName,
                                this.webset.watermarktext, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarkfont, this.webset.watermarkfontsize);
                            break;
                        case 2:
                            WaterMark.AddImageSignPic(serverFileName, serverFileName,
                                this.webset.watermarkpic, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarktransparency);
                            break;
                    }
                }
                return Json(1, serverFileName);
            }
            catch
            {
                return Json(0, "上传过程中发生意外错误！");
            }
        }

        ///<summary>
        /// 文件上传方法(根据栏目命名保存路径名称)
        /// </summary>
        public string fileSaveAs(HttpPostedFile postedFile, string channel_name, int category_id)
        {
            try
            {
                string fileExt = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(".") + 1);

                //验证合法的文件
                if (!FileHelper.CheckFileExt(this.fileType, fileExt))
                {
                    return Json(0, "不允许上传" + fileExt + "类型的文件！");
                }
                if (this.fileSize > 0 && postedFile.ContentLength > fileSize * 1024)
                {
                    return Json(0, "文件超过限制的大小啦！");
                }
                string fileName = FileHelper.MakeFileName(fileExt); //随机文件名
                this.filePath = FileHelper.MakeUploadDire(webset.web_path, channel_name, category_id);
                //检查保存的路径 是否有/开头结尾
                if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
                if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
                //获得要保存的文件路径
                string serverFileName = this.filePath + fileName;
                //物理完整路径                    
                string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
                //将要保存的完整文件名                
                string toFile = toFileFullPath + fileName;
                //保存文件
                postedFile.SaveAs(toFile);

                //是否打图片水印
                if (IsWaterMark(fileExt) && waterMarkType > 0)
                {
                    switch (this.webset.watermarktype)
                    {
                        case 1:
                            WaterMark.AddImageSignText(serverFileName, serverFileName,
                                this.webset.watermarktext, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarkfont, this.webset.watermarkfontsize);
                            break;
                        case 2:
                            WaterMark.AddImageSignPic(serverFileName, serverFileName,
                                this.webset.watermarkpic, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarktransparency);
                            break;
                    }
                }
                return Json(1, serverFileName);
            }
            catch
            {
                return Json(0, "上传过程中发生意外错误！");
            }
        }

        ///<summary>
        /// 文件上传方法（编辑框图片上传）
        /// </summary>
        public string editor_file_saveAs(HttpPostedFile _postedFile)
        {
            try
            {
                string _fileExt = FileHelper.GetFileExt(_postedFile.FileName);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(this.fileType, _fileExt))
                {
                    return "{msg: 0, msbox: \"不允许上传" + _fileExt + "类型的文件！\"}";
                }
                if (this.fileSize > 0 && _postedFile.ContentLength > fileSize * 1024)
                {
                    return "{msg: 0, msbox: \"文件超过限制的大小啦！\"}";
                }
                string fileName = FileHelper.MakeFileName(_fileExt); //随机文件名
                this.filePath = FileHelper.MakeUploadDire(webset.web_path, "editor", 0);
                //检查保存的路径 是否有/开头结尾
                if (this.filePath.StartsWith("/") == false) this.filePath = "/" + this.filePath;
                if (this.filePath.EndsWith("/") == false) this.filePath = this.filePath + "/";
                //获得要保存的文件路径
                string serverFileName = this.filePath + fileName;
                //物理完整路径                    
                string toFileFullPath = HttpContext.Current.Server.MapPath(this.filePath);
                //将要保存的完整文件名                
                string toFile = toFileFullPath + fileName;
                //保存文件
                _postedFile.SaveAs(toFile);

                //是否打图片水印
                if (waterMarkType > 0 && FileHelper.CheckFileExt("BMP|JPEG|JPG|GIF|PNG|TIFF", _fileExt))
                {
                    switch (waterMarkType)
                    {
                        case 1:
                            WaterMark.AddImageSignText(serverFileName, serverFileName,
                                this.webset.watermarktext, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarkfont, this.webset.watermarkfontsize);
                            break;
                        case 2:
                            WaterMark.AddImageSignPic(serverFileName, serverFileName,
                                this.webset.watermarkpic, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarktransparency);
                            break;
                    }
                }
                return "{msg: 1, msbox: \"" + serverFileName + "\"}";
            }
            catch
            {
                return "{msg: 0, msbox: \"上传过程中发生意外错误！\"}";
            }
        }

        /// <summary>
        /// 是否需要打水印
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        private bool IsWaterMark(string _fileExt)
        {
            //判断是否开启水印
            if (this.webset.watermarktype > 0)
            {
                //判断是否可以打水印的图片类型
                ArrayList al = new ArrayList();
                al.Add("bmp");
                al.Add("jpeg");
                al.Add("jpg");
                al.Add("png");
                if (al.Contains(_fileExt.ToLower()))
                {
                    return true;
                }
            }
            return false;
        }


        private string Json(int msg, string msbox)
        {
            return "{\"msg\": \"" + msg + "\", \"msbox\": \"" + msbox + "\"}";
        }

    }
}
